<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        let input = `0,0,0,0
        0,1,0,0
        0,0,0,0
        0,0,0,0`

        if (!input.includes('0') || !input.includes('1')) {
            console.log(-1);
        } else {
            let map = input.split('\n').map(v => v.trim().split(',').map(t => parseInt(t)))
            let len = map.length
            let max = len * len
            let count = 0
            let day = 0
            let index = []

            function count_1() {
                let ct = 0
                for (let i = 0; i < len; i++) {
                    for (let j = 0; j < len; j++) {
                        if (map[i][j]) {
                            ct++
                            index.push([i, j])
                        }

                    }
                }

                count = ct

            }
            


            function ganran() {
                while (index.length > 0) {
                    let [i, j] = index.pop()
                    if (i - 1 >= 0) {
                        map[i - 1][j] = 1
                    }
                    if (j + 1 < len) {
                        map[i][j + 1] = 1
                    }
                    if (i + 1 < len) {
                        map[i + 1][j] = 1
                    }
                    if (j - 1 >= 0) {
                        map[i][j - 1] = 1
                    }
                }
            }

            
            count_1()

            while (count < max) {
                ganran()                
                count_1()
                
                console.log(...map);
                
                count++
                day++
            }
            console.log(day);



        }
    </script>
</body>

</html>